package com.crm.service.impl;

import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.common.result.ResultVO;
import com.crm.common.result.TableDataInfo;
import com.crm.DTO.CoursesDto;
import com.crm.common.utils.RandomStringGenerator;
import com.crm.mapper.CoursesMapper;
import com.crm.service.CoursesService;

import com.crm.entity.Courses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 2025/9/4 11:15
 * 注释：
 */

@Service
@Slf4j
public class CoursesServiceImpl extends ServiceImpl<CoursesMapper, Courses> implements CoursesService {


    @Autowired
    private CoursesMapper coursesMapper;


    /**
     * 课程列表(分页查询)
     *
     * @param dto
     * @return
     */
    @Override
    public TableDataInfo<Courses> searchCourses(CoursesDto dto) {
        // 创建分页对象
        Page<Courses> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        // 创建查询条件
        LambdaQueryWrapper<Courses> wrapper = new LambdaQueryWrapper<>();
        // 根据课程名称模糊查询
/*        if (dto.getName() != null && dto.getName() != ""){
            wrapper.like(Courses::getName, dto.getName());}*/
        wrapper.like(dto.getName() != null && dto.getName() != "", Courses::getName, dto.getName());
        wrapper.eq(dto.getCode() != null && dto.getCode() != "", Courses::getCode, dto.getCode());
        wrapper.eq(dto.getSubject() != null && dto.getSubject() != "", Courses::getSubject, dto.getSubject());
        wrapper.eq(dto.getApplicablePerson() != null && dto.getApplicablePerson() != "", Courses::getApplicablePerson, dto.getApplicablePerson());
        wrapper.between(dto.getBeginCreateTime() != null && dto.getEndCreateTime() != null, Courses::getCreateTime, dto.getBeginCreateTime(), dto.getEndCreateTime());

        // 分页查询(查询数据)
        Page<Courses> pageData = coursesMapper.selectPage(page, wrapper);
        // 构造返回结果VO
        TableDataInfo<Courses> coursesTableDataInfo = new TableDataInfo<>();
        coursesTableDataInfo.setTotal(pageData.getTotal());
        coursesTableDataInfo.setRows(pageData.getRecords());
        coursesTableDataInfo.setCode(200);
        coursesTableDataInfo.setMsg("操作成功");
        return coursesTableDataInfo;
    }

    /**
     * 删除课程
     *
     * @param coursesId
     */
    @Override
    public ResultVO deleteCourses(String coursesId) {
        coursesMapper.delete(Wrappers.<Courses>lambdaQuery().eq(Courses::getId, coursesId));
        return ResultVO.success();
    }

    /**
     * 修改课程
     */
    @Override
    public ResultVO updateCourses(CoursesDto coursesDto) {
        Courses courses = new Courses();
        BeanUtils.copyProperties(coursesDto, courses);
        super.updateById(courses);
        return ResultVO.success();
    }

    /**
     * 获取课程
     *
     * @param coursesId
     * @return
     */
    @Override
    public ResultVO<Courses> getCourses(String coursesId) {
        // 查询课程
        Courses courses = coursesMapper.selectOne(Wrappers.<Courses>lambdaQuery().eq(Courses::getId, coursesId));
        // 判断课程是否为空
        if (courses != null) {
            return ResultVO.success(courses);
        }
        return ResultVO.error();
    }

    /**
     * 保存课程
     *
     * @param coursesDto
     */
    @Override
    public ResultVO saveCourses(CoursesDto coursesDto) {
        Courses courses = new Courses();
        BeanUtils.copyProperties(coursesDto, courses);
        courses.setCode(RandomStringGenerator.generateRandomString());
        coursesMapper.insert(courses);
        return ResultVO.success();
    }

    /**
     * 查询学科下对应的课程列表
     * @param subject
     * @return
     */
    @Override
    public List<Courses> listCoursesBySubject(Integer subject) {
        LambdaQueryWrapper<Courses> wrapper = new LambdaQueryWrapper<>();
        if(subject != null){
            wrapper.eq(Courses::getSubject,subject);
        }
        List<Courses> list = coursesMapper.selectList(wrapper);
        return list;
    }
}
